function createAjax(){
    var ajax = false;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        ajax=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        ajax=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return ajax;
}
var page = 1;
var pageSize = 3;

function getMoreImg1(){
    alert('success');
}

function getMoreImg()
{
    page += 1;
    var ajax = createAjax();
    var url = '/profile/images/' + window.uid + '/' + page + '/' + pageSize + '/';
    ajax.open('get', url, false);
    ajax.onreadystatechange = function(){
        if(ajax.readyState==4){
            if(ajax.status==200){
                var sHTML = '';
                imgJson = eval('('+ajax.responseText+')');
                for(var i=0;i<imgJson.images.length;i++){
                    //alert(imgJson.images[i].url);
                    sHTML += [  '<a class="item" href="/image/'+imgJson.images[i].id+'/">',
                                '<div class="img-box">',
                                '<img src="'+ imgJson.images[i].url+'">',
                                '</div>',
                                '</a>'
                             ].join('');
                }
                var imgList = document.getElementById('img_lists');
                var nextImgList = document.createElement('a');
                nextImgList.innerHTML = sHTML;
                imgList.appendChild(nextImgList);
            }
        }
        if(imgJson.has_next == false){
            document.getElementById("more").style.display = "none";
        }
    }
    ajax.send(null);
}

function submitImg()
{
    var formObj = new FormData(document.getElementById('form'));
    var ajax = new XMLHttpRequest();
    ajax.open('post','/upload/',false);
    ajax.send(formObj);
    if(ajax.readyState==4 && ajax.status==200){
        alert("succeed");
    }
    //document.getElementById("s-num").innerText = parseInt(document.getElementById("s-num").innerText) + 1;
    //addFirstImg();

}

function addFirstImg(){

    var ajax = createAjax();
    var url = '/profile/images/' + window.uid + '/' + 1 + '/' + 1 + '/';
    ajax.open('get', url, false);
    var sHTML = '';
    imgJson = eval('('+ajax.responseText+')');
    sHTML += [  '<a class="item" href="/image/'+imgJson.images[0].id+'/">',
                '<div class="img-box">',
                '<img src="'+ imgJson.images[0].url+'">',
                '</div>',
                '</a>'
             ].join('');
    alert(sHTML);
    var imgList = document.getElementById('img_lists');
    var oldNode = imgList.firstChild;
    var newNode = document.createElement('a');
    newNode.innerHTML = sHTML;
    imgList.insertBefore(newNode,oldNode);
}
